home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 1998 November / Freeware November 1998.img / dist / fw_elisp-manual-19.idb / usr / freeware / info / elisp-4.z / elisp-4 (.txt)
GNU Info File  |  1998-05-26  |  47KB  |  922 lines

  1. This is Info file elisp, produced by Makeinfo-1.63 from the input file
  2. elisp.texi.
  3.    This version is the edition 2.4.2 of the GNU Emacs Lisp Reference
  4. Manual.  It corresponds to Emacs Version 19.34.
  5.    Published by the Free Software Foundation 59 Temple Place, Suite 330
  6. Boston, MA  02111-1307  USA
  7.    Copyright (C) 1990, 1991, 1992, 1993, 1994, 1995, 1996 Free Software
  8. Foundation, Inc.
  9.    Permission is granted to make and distribute verbatim copies of this
  10. manual provided the copyright notice and this permission notice are
  11. preserved on all copies.
  12.    Permission is granted to copy and distribute modified versions of
  13. this manual under the conditions for verbatim copying, provided that the
  14. entire resulting derived work is distributed under the terms of a
  15. permission notice identical to this one.
  16.    Permission is granted to copy and distribute translations of this
  17. manual into another language, under the above conditions for modified
  18. versions, except that this permission notice may be stated in a
  19. translation approved by the Foundation.
  20.    Permission is granted to copy and distribute modified versions of
  21. this manual under the conditions for verbatim copying, provided also
  22. that the section entitled "GNU General Public License" is included
  23. exactly as in the original, and provided that the entire resulting
  24. derived work is distributed under the terms of a permission notice
  25. identical to this one.
  26.    Permission is granted to copy and distribute translations of this
  27. manual into another language, under the above conditions for modified
  28. versions, except that the section entitled "GNU General Public License"
  29. may be included in a translation approved by the Free Software
  30. Foundation instead of in the original English.
  31. File: elisp,  Node: Integer Basics,  Next: Float Basics,  Up: Numbers
  32. Integer Basics
  33. ==============
  34.    The range of values for an integer depends on the machine.  The
  35. minimum range is -134217728 to 134217727 (28 bits; i.e., -2**27 to
  36. 2**27 - 1), but some machines may provide a wider range.  Many examples
  37. in this chapter assume an integer has 28 bits.
  38.    The Lisp reader reads an integer as a sequence of digits with
  39. optional initial sign and optional final period.
  40.       1               ; The integer 1.
  41.       1.              ; The integer 1.
  42.      +1               ; Also the integer 1.
  43.      -1               ; The integer -1.
  44.       268435457       ; Also the integer 1, due to overflow.
  45.       0               ; The integer 0.
  46.      -0               ; The integer 0.
  47.    To understand how various functions work on integers, especially the
  48. bitwise operators (*note Bitwise Operations::.), it is often helpful to
  49. view the numbers in their binary form.
  50.    In 28-bit binary, the decimal integer 5 looks like this:
  51.      0000  0000 0000  0000 0000  0000 0101
  52. (We have inserted spaces between groups of 4 bits, and two spaces
  53. between groups of 8 bits, to make the binary integer easier to read.)
  54.    The integer -1 looks like this:
  55.      1111  1111 1111  1111 1111  1111 1111
  56. -1 is represented as 28 ones.  (This is called "two's complement"
  57. notation.)
  58.    The negative integer, -5, is creating by subtracting 4 from -1.  In
  59. binary, the decimal integer 4 is 100.  Consequently, -5 looks like this:
  60.      1111  1111 1111  1111 1111  1111 1011
  61.    In this implementation, the largest 28-bit binary integer value is
  62. 134,217,727.  In binary, it looks like this:
  63.      0111  1111 1111  1111 1111  1111 1111
  64.    Since the arithmetic functions do not check whether integers go
  65. outside their range, when you add 1 to 134,217,727, the value is the
  66. negative integer -134,217,728:
  67.      (+ 1 134217727)
  68.           => -134217728
  69.           => 1000  0000 0000  0000 0000  0000 0000
  70.    Many of the following functions accept markers for arguments as well
  71. as integers.  (*Note Markers::.)  More precisely, the actual arguments
  72. to such functions may be either integers or markers, which is why we
  73. often give these arguments the name INT-OR-MARKER.  When the argument
  74. value is a marker, its position value is used and its buffer is ignored.
  75. File: elisp,  Node: Float Basics,  Next: Predicates on Numbers,  Prev: Integer Basics,  Up: Numbers
  76. Floating Point Basics
  77. =====================
  78.    Emacs version 19 supports floating point numbers, if compiled with
  79. the macro `LISP_FLOAT_TYPE' defined.  The precise range of floating
  80. point numbers is machine-specific; it is the same as the range of the C
  81. data type `double' on the machine in question.
  82.    The printed representation for floating point numbers requires either
  83. a decimal point (with at least one digit following), an exponent, or
  84. both.  For example, `1500.0', `15e2', `15.0e2', `1.5e3', and `.15e4'
  85. are five ways of writing a floating point number whose value is 1500.
  86. They are all equivalent.  You can also use a minus sign to write
  87. negative floating point numbers, as in `-1.0'.
  88.    Most modern computers support the IEEE floating point standard, which
  89. provides for positive infinity and negative infinity as floating point
  90. values.  It also provides for a class of values called NaN or
  91. "not-a-number"; numerical functions return such values in cases where
  92. there is no correct answer.  For example, `(sqrt -1.0)' returns a NaN.
  93. For practical purposes, there's no significant difference between
  94. different NaN values in Emacs Lisp, and there's no rule for precisely
  95. which NaN value should be used in a particular case, so this manual
  96. doesn't try to distinguish them.  Emacs Lisp has no read syntax for NaNs
  97. or infinities; perhaps we should create a syntax in the future.
  98.    You can use `logb' to extract the binary exponent of a floating
  99. point number (or estimate the logarithm of an integer):
  100.  - Function: logb NUMBER
  101.      This function returns the binary exponent of NUMBER.  More
  102.      precisely, the value is the logarithm of NUMBER base 2, rounded
  103.      down to an integer.
  104. File: elisp,  Node: Predicates on Numbers,  Next: Comparison of Numbers,  Prev: Float Basics,  Up: Numbers
  105. Type Predicates for Numbers
  106. ===========================
  107.    The functions in this section test whether the argument is a number
  108. or whether it is a certain sort of number.  The functions `integerp'
  109. and `floatp' can take any type of Lisp object as argument (the
  110. predicates would not be of much use otherwise); but the `zerop'
  111. predicate requires a number as its argument.  See also
  112. `integer-or-marker-p' and `number-or-marker-p', in *Note Predicates on
  113. Markers::.
  114.  - Function: floatp OBJECT
  115.      This predicate tests whether its argument is a floating point
  116.      number and returns `t' if so, `nil' otherwise.
  117.      `floatp' does not exist in Emacs versions 18 and earlier.
  118.  - Function: integerp OBJECT
  119.      This predicate tests whether its argument is an integer, and
  120.      returns `t' if so, `nil' otherwise.
  121.  - Function: numberp OBJECT
  122.      This predicate tests whether its argument is a number (either
  123.      integer or floating point), and returns `t' if so, `nil' otherwise.
  124.  - Function: wholenump OBJECT
  125.      The `wholenump' predicate (whose name comes from the phrase
  126.      "whole-number-p") tests to see whether its argument is a
  127.      nonnegative integer, and returns `t' if so, `nil' otherwise.  0 is
  128.      considered non-negative.
  129.      `natnump' is an obsolete synonym for `wholenump'.
  130.  - Function: zerop NUMBER
  131.      This predicate tests whether its argument is zero, and returns `t'
  132.      if so, `nil' otherwise.  The argument must be a number.
  133.      These two forms are equivalent: `(zerop x)' == `(= x 0)'.
  134. File: elisp,  Node: Comparison of Numbers,  Next: Numeric Conversions,  Prev: Predicates on Numbers,  Up: Numbers
  135. Comparison of Numbers
  136. =====================
  137.    To test numbers for numerical equality, you should normally use `=',
  138. not `eq'.  There can be many distinct floating point number objects
  139. with the same numeric value.  If you use `eq' to compare them, then you
  140. test whether two values are the same *object*.  By contrast, `='
  141. compares only the numeric values of the objects.
  142.    At present, each integer value has a unique Lisp object in Emacs
  143. Lisp.  Therefore, `eq' is equivalent `=' where integers are concerned.
  144. It is sometimes convenient to use `eq' for comparing an unknown value
  145. with an integer, because `eq' does not report an error if the unknown
  146. value is not a number--it accepts arguments of any type.  By contrast,
  147. `=' signals an error if the arguments are not numbers or markers.
  148. However, it is a good idea to use `=' if you can, even for comparing
  149. integers, just in case we change the representation of integers in a
  150. future Emacs version.
  151.    There is another wrinkle: because floating point arithmetic is not
  152. exact, it is often a bad idea to check for equality of two floating
  153. point values.  Usually it is better to test for approximate equality.
  154. Here's a function to do this:
  155.      (defvar fuzz-factor 1.0e-6)
  156.      (defun approx-equal (x y)
  157.        (or (and (= x 0) (= y 0))
  158.            (< (/ (abs (- x y))
  159.                  (max (abs x) (abs y)))
  160.               fuzz-factor)))
  161.      Common Lisp note: Comparing numbers in Common Lisp always requires
  162.      `=' because Common Lisp implements multi-word integers, and two
  163.      distinct integer objects can have the same numeric value.  Emacs
  164.      Lisp can have just one integer object for any given value because
  165.      it has a limited range of integer values.
  166.  - Function: = NUMBER-OR-MARKER1 NUMBER-OR-MARKER2
  167.      This function tests whether its arguments are numerically equal,
  168.      and returns `t' if so, `nil' otherwise.
  169.  - Function: /= NUMBER-OR-MARKER1 NUMBER-OR-MARKER2
  170.      This function tests whether its arguments are numerically equal,
  171.      and returns `t' if they are not, and `nil' if they are.
  172.  - Function: < NUMBER-OR-MARKER1 NUMBER-OR-MARKER2
  173.      This function tests whether its first argument is strictly less
  174.      than its second argument.  It returns `t' if so, `nil' otherwise.
  175.  - Function: <= NUMBER-OR-MARKER1 NUMBER-OR-MARKER2
  176.      This function tests whether its first argument is less than or
  177.      equal to its second argument.  It returns `t' if so, `nil'
  178.      otherwise.
  179.  - Function: > NUMBER-OR-MARKER1 NUMBER-OR-MARKER2
  180.      This function tests whether its first argument is strictly greater
  181.      than its second argument.  It returns `t' if so, `nil' otherwise.
  182.  - Function: >= NUMBER-OR-MARKER1 NUMBER-OR-MARKER2
  183.      This function tests whether its first argument is greater than or
  184.      equal to its second argument.  It returns `t' if so, `nil'
  185.      otherwise.
  186.  - Function: max NUMBER-OR-MARKER &rest NUMBERS-OR-MARKERS
  187.      This function returns the largest of its arguments.
  188.           (max 20)
  189.                => 20
  190.           (max 1 2.5)
  191.                => 2.5
  192.           (max 1 3 2.5)
  193.                => 3
  194.  - Function: min NUMBER-OR-MARKER &rest NUMBERS-OR-MARKERS
  195.      This function returns the smallest of its arguments.
  196.           (min -4 1)
  197.                => -4
  198. File: elisp,  Node: Numeric Conversions,  Next: Arithmetic Operations,  Prev: Comparison of Numbers,  Up: Numbers
  199. Numeric Conversions
  200. ===================
  201.    To convert an integer to floating point, use the function `float'.
  202.  - Function: float NUMBER
  203.      This returns NUMBER converted to floating point.  If NUMBER is
  204.      already a floating point number, `float' returns it unchanged.
  205.    There are four functions to convert floating point numbers to
  206. integers; they differ in how they round.  These functions accept
  207. integer arguments also, and return such arguments unchanged.
  208.  - Function: truncate NUMBER
  209.      This returns NUMBER, converted to an integer by rounding towards
  210.      zero.
  211.  - Function: floor NUMBER &optional DIVISOR
  212.      This returns NUMBER, converted to an integer by rounding downward
  213.      (towards negative infinity).
  214.      If DIVISOR is specified, NUMBER is divided by DIVISOR before the
  215.      floor is taken; this is the division operation that corresponds to
  216.      `mod'.  An `arith-error' results if DIVISOR is 0.
  217.  - Function: ceiling NUMBER
  218.      This returns NUMBER, converted to an integer by rounding upward
  219.      (towards positive infinity).
  220.  - Function: round NUMBER
  221.      This returns NUMBER, converted to an integer by rounding towards
  222.      the nearest integer.  Rounding a value equidistant between two
  223.      integers may choose the integer closer to zero, or it may prefer
  224.      an even integer, depending on your machine.
  225. File: elisp,  Node: Arithmetic Operations,  Next: Rounding Operations,  Prev: Numeric Conversions,  Up: Numbers
  226. Arithmetic Operations
  227. =====================
  228.    Emacs Lisp provides the traditional four arithmetic operations:
  229. addition, subtraction, multiplication, and division.  Remainder and
  230. modulus functions supplement the division functions.  The functions to
  231. add or subtract 1 are provided because they are traditional in Lisp and
  232. commonly used.
  233.    All of these functions except `%' return a floating point value if
  234. any argument is floating.
  235.    It is important to note that in GNU Emacs Lisp, arithmetic functions
  236. do not check for overflow.  Thus `(1+ 134217727)' may evaluate to
  237. -134217728, depending on your hardware.
  238.  - Function: 1+ NUMBER-OR-MARKER
  239.      This function returns NUMBER-OR-MARKER plus 1.  For example,
  240.           (setq foo 4)
  241.                => 4
  242.           (1+ foo)
  243.                => 5
  244.      This function is not analogous to the C operator `++'--it does not
  245.      increment a variable.  It just computes a sum.  Thus, if we
  246.      continue,
  247.           foo
  248.                => 4
  249.      If you want to increment the variable, you must use `setq', like
  250.      this:
  251.           (setq foo (1+ foo))
  252.                => 5
  253.  - Function: 1- NUMBER-OR-MARKER
  254.      This function returns NUMBER-OR-MARKER minus 1.
  255.  - Function: abs NUMBER
  256.      This returns the absolute value of NUMBER.
  257.  - Function: + &rest NUMBERS-OR-MARKERS
  258.      This function adds its arguments together.  When given no
  259.      arguments, `+' returns 0.
  260.           (+)
  261.                => 0
  262.           (+ 1)
  263.                => 1
  264.           (+ 1 2 3 4)
  265.                => 10
  266.  - Function: - &optional NUMBER-OR-MARKER &rest OTHER-NUMBERS-OR-MARKERS
  267.      The `-' function serves two purposes: negation and subtraction.
  268.      When `-' has a single argument, the value is the negative of the
  269.      argument.  When there are multiple arguments, `-' subtracts each of
  270.      the OTHER-NUMBERS-OR-MARKERS from NUMBER-OR-MARKER, cumulatively.
  271.      If there are no arguments, the result is 0.
  272.           (- 10 1 2 3 4)
  273.                => 0
  274.           (- 10)
  275.                => -10
  276.           (-)
  277.                => 0
  278.  - Function: * &rest NUMBERS-OR-MARKERS
  279.      This function multiplies its arguments together, and returns the
  280.      product.  When given no arguments, `*' returns 1.
  281.           (*)
  282.                => 1
  283.           (* 1)
  284.                => 1
  285.           (* 1 2 3 4)
  286.                => 24
  287.  - Function: / DIVIDEND DIVISOR &rest DIVISORS
  288.      This function divides DIVIDEND by DIVISOR and returns the
  289.      quotient.  If there are additional arguments DIVISORS, then it
  290.      divides DIVIDEND by each divisor in turn.  Each argument may be a
  291.      number or a marker.
  292.      If all the arguments are integers, then the result is an integer
  293.      too.  This means the result has to be rounded.  On most machines,
  294.      the result is rounded towards zero after each division, but some
  295.      machines may round differently with negative arguments.  This is
  296.      because the Lisp function `/' is implemented using the C division
  297.      operator, which also permits machine-dependent rounding.  As a
  298.      practical matter, all known machines round in the standard fashion.
  299.      If you divide by 0, an `arith-error' error is signaled.  (*Note
  300.      Errors::.)
  301.           (/ 6 2)
  302.                => 3
  303.           (/ 5 2)
  304.                => 2
  305.           (/ 25 3 2)
  306.                => 4
  307.           (/ -17 6)
  308.                => -2
  309.      The result of `(/ -17 6)' could in principle be -3 on some
  310.      machines.
  311.  - Function: % DIVIDEND DIVISOR
  312.      This function returns the integer remainder after division of
  313.      DIVIDEND by DIVISOR.  The arguments must be integers or markers.
  314.      For negative arguments, the remainder is in principle
  315.      machine-dependent since the quotient is; but in practice, all
  316.      known machines behave alike.
  317.      An `arith-error' results if DIVISOR is 0.
  318.           (% 9 4)
  319.                => 1
  320.           (% -9 4)
  321.                => -1
  322.           (% 9 -4)
  323.                => 1
  324.           (% -9 -4)
  325.                => -1
  326.      For any two integers DIVIDEND and DIVISOR,
  327.           (+ (% DIVIDEND DIVISOR)
  328.              (* (/ DIVIDEND DIVISOR) DIVISOR))
  329.      always equals DIVIDEND.
  330.  - Function: mod DIVIDEND DIVISOR
  331.      This function returns the value of DIVIDEND modulo DIVISOR; in
  332.      other words, the remainder after division of DIVIDEND by DIVISOR,
  333.      but with the same sign as DIVISOR.  The arguments must be numbers
  334.      or markers.
  335.      Unlike `%', `mod' returns a well-defined result for negative
  336.      arguments.  It also permits floating point arguments; it rounds the
  337.      quotient downward (towards minus infinity) to an integer, and uses
  338.      that quotient to compute the remainder.
  339.      An `arith-error' results if DIVISOR is 0.
  340.           (mod 9 4)
  341.                => 1
  342.           (mod -9 4)
  343.                => 3
  344.           (mod 9 -4)
  345.                => -3
  346.           (mod -9 -4)
  347.                => -1
  348.           (mod 5.5 2.5)
  349.                => .5
  350.      For any two numbers DIVIDEND and DIVISOR,
  351.           (+ (mod DIVIDEND DIVISOR)
  352.              (* (floor DIVIDEND DIVISOR) DIVISOR))
  353.      always equals DIVIDEND, subject to rounding error if either
  354.      argument is floating point.  For `floor', see *Note Numeric
  355.      Conversions::.
  356. File: elisp,  Node: Rounding Operations,  Next: Bitwise Operations,  Prev: Arithmetic Operations,  Up: Numbers
  357. Rounding Operations
  358. ===================
  359.    The functions `ffloor', `fceiling', `fround' and `ftruncate' take a
  360. floating point argument and return a floating point result whose value
  361. is a nearby integer.  `ffloor' returns the nearest integer below;
  362. `fceiling', the nearest integer above; `ftruncate', the nearest integer
  363. in the direction towards zero; `fround', the nearest integer.
  364.  - Function: ffloor FLOAT
  365.      This function rounds FLOAT to the next lower integral value, and
  366.      returns that value as a floating point number.
  367.  - Function: fceiling FLOAT
  368.      This function rounds FLOAT to the next higher integral value, and
  369.      returns that value as a floating point number.
  370.  - Function: ftruncate FLOAT
  371.      This function rounds FLOAT towards zero to an integral value, and
  372.      returns that value as a floating point number.
  373.  - Function: fround FLOAT
  374.      This function rounds FLOAT to the nearest integral value, and
  375.      returns that value as a floating point number.
  376. File: elisp,  Node: Bitwise Operations,  Next: Math Functions,  Prev: Rounding Operations,  Up: Numbers
  377. Bitwise Operations on Integers
  378. ==============================
  379.    In a computer, an integer is represented as a binary number, a
  380. sequence of "bits" (digits which are either zero or one).  A bitwise
  381. operation acts on the individual bits of such a sequence.  For example,
  382. "shifting" moves the whole sequence left or right one or more places,
  383. reproducing the same pattern "moved over".
  384.    The bitwise operations in Emacs Lisp apply only to integers.
  385.  - Function: lsh INTEGER1 COUNT
  386.      `lsh', which is an abbreviation for "logical shift", shifts the
  387.      bits in INTEGER1 to the left COUNT places, or to the right if
  388.      COUNT is negative, bringing zeros into the vacated bits.  If COUNT
  389.      is negative, `lsh' shifts zeros into the leftmost
  390.      (most-significant) bit, producing a positive result even if
  391.      INTEGER1 is negative.  Contrast this with `ash', below.
  392.      Here are two examples of `lsh', shifting a pattern of bits one
  393.      place to the left.  We show only the low-order eight bits of the
  394.      binary pattern; the rest are all zero.
  395.           (lsh 5 1)
  396.                => 10
  397.           ;; Decimal 5 becomes decimal 10.
  398.           00000101 => 00001010
  399.           
  400.           (lsh 7 1)
  401.                => 14
  402.           ;; Decimal 7 becomes decimal 14.
  403.           00000111 => 00001110
  404.      As the examples illustrate, shifting the pattern of bits one place
  405.      to the left produces a number that is twice the value of the
  406.      previous number.
  407.      Shifting a pattern of bits two places to the left produces results
  408.      like this (with 8-bit binary numbers):
  409.           (lsh 3 2)
  410.                => 12
  411.           ;; Decimal 3 becomes decimal 12.
  412.           00000011 => 00001100
  413.      On the other hand, shifting one place to the right looks like this:
  414.           (lsh 6 -1)
  415.                => 3
  416.           ;; Decimal 6 becomes decimal 3.
  417.           00000110 => 00000011
  418.           
  419.           (lsh 5 -1)
  420.                => 2
  421.           ;; Decimal 5 becomes decimal 2.
  422.           00000101 => 00000010
  423.      As the example illustrates, shifting one place to the right
  424.      divides the value of a positive integer by two, rounding downward.
  425.      The function `lsh', like all Emacs Lisp arithmetic functions, does
  426.      not check for overflow, so shifting left can discard significant
  427.      bits and change the sign of the number.  For example, left shifting
  428.      134,217,727 produces -2 on a 28-bit machine:
  429.           (lsh 134217727 1)          ; left shift
  430.                => -2
  431.      In binary, in the 28-bit implementation, the argument looks like
  432.      this:
  433.           ;; Decimal 134,217,727
  434.           0111  1111 1111  1111 1111  1111 1111
  435.      which becomes the following when left shifted:
  436.           ;; Decimal -2
  437.           1111  1111 1111  1111 1111  1111 1110
  438.  - Function: ash INTEGER1 COUNT
  439.      `ash' ("arithmetic shift") shifts the bits in INTEGER1 to the left
  440.      COUNT places, or to the right if COUNT is negative.
  441.      `ash' gives the same results as `lsh' except when INTEGER1 and
  442.      COUNT are both negative.  In that case, `ash' puts ones in the
  443.      empty bit positions on the left, while `lsh' puts zeros in those
  444.      bit positions.
  445.      Thus, with `ash', shifting the pattern of bits one place to the
  446.      right looks like this:
  447.           (ash -6 -1) => -3
  448.           ;; Decimal -6 becomes decimal -3.
  449.           1111  1111 1111  1111 1111  1111 1010
  450.                =>
  451.           1111  1111 1111  1111 1111  1111 1101
  452.      In contrast, shifting the pattern of bits one place to the right
  453.      with `lsh' looks like this:
  454.           (lsh -6 -1) => 134217725
  455.           ;; Decimal -6 becomes decimal 134,217,725.
  456.           1111  1111 1111  1111 1111  1111 1010
  457.                =>
  458.           0111  1111 1111  1111 1111  1111 1101
  459.      Here are other examples:
  460.           ;               28-bit binary values
  461.           
  462.           (lsh 5 2)          ;   5  =  0000  0000 0000  0000 0000  0000 0101
  463.                => 20         ;      =  0000  0000 0000  0000 0000  0001 0100
  464.           (ash 5 2)
  465.                => 20
  466.           (lsh -5 2)         ;  -5  =  1111  1111 1111  1111 1111  1111 1011
  467.                => -20        ;      =  1111  1111 1111  1111 1111  1110 1100
  468.           (ash -5 2)
  469.                => -20
  470.           (lsh 5 -2)         ;   5  =  0000  0000 0000  0000 0000  0000 0101
  471.                => 1          ;      =  0000  0000 0000  0000 0000  0000 0001
  472.           (ash 5 -2)
  473.                => 1
  474.           (lsh -5 -2)        ;  -5  =  1111  1111 1111  1111 1111  1111 1011
  475.                => 4194302    ;      =  0011  1111 1111  1111 1111  1111 1110
  476.           (ash -5 -2)        ;  -5  =  1111  1111 1111  1111 1111  1111 1011
  477.                => -2         ;      =  1111  1111 1111  1111 1111  1111 1110
  478.  - Function: logand &rest INTS-OR-MARKERS
  479.      This function returns the "logical and" of the arguments: the Nth
  480.      bit is set in the result if, and only if, the Nth bit is set in
  481.      all the arguments.  ("Set" means that the value of the bit is 1
  482.      rather than 0.)
  483.      For example, using 4-bit binary numbers, the "logical and" of 13
  484.      and 12 is 12: 1101 combined with 1100 produces 1100.  In both the
  485.      binary numbers, the leftmost two bits are set (i.e., they are
  486.      1's), so the leftmost two bits of the returned value are set.
  487.      However, for the rightmost two bits, each is zero in at least one
  488.      of the arguments, so the rightmost two bits of the returned value
  489.      are 0's.
  490.      Therefore,
  491.           (logand 13 12)
  492.                => 12
  493.      If `logand' is not passed any argument, it returns a value of -1.
  494.      This number is an identity element for `logand' because its binary
  495.      representation consists entirely of ones.  If `logand' is passed
  496.      just one argument, it returns that argument.
  497.           ;                28-bit binary values
  498.           
  499.           (logand 14 13)     ; 14  =  0000  0000 0000  0000 0000  0000 1110
  500.                              ; 13  =  0000  0000 0000  0000 0000  0000 1101
  501.                => 12         ; 12  =  0000  0000 0000  0000 0000  0000 1100
  502.           (logand 14 13 4)   ; 14  =  0000  0000 0000  0000 0000  0000 1110
  503.                              ; 13  =  0000  0000 0000  0000 0000  0000 1101
  504.                              ;  4  =  0000  0000 0000  0000 0000  0000 0100
  505.                => 4          ;  4  =  0000  0000 0000  0000 0000  0000 0100
  506.           (logand)
  507.                => -1         ; -1  =  1111  1111 1111  1111 1111  1111 1111
  508.  - Function: logior &rest INTS-OR-MARKERS
  509.      This function returns the "inclusive or" of its arguments: the Nth
  510.      bit is set in the result if, and only if, the Nth bit is set in at
  511.      least one of the arguments.  If there are no arguments, the result
  512.      is zero, which is an identity element for this operation.  If
  513.      `logior' is passed just one argument, it returns that argument.
  514.           ;               28-bit binary values
  515.           
  516.           (logior 12 5)      ; 12  =  0000  0000 0000  0000 0000  0000 1100
  517.                              ;  5  =  0000  0000 0000  0000 0000  0000 0101
  518.                => 13         ; 13  =  0000  0000 0000  0000 0000  0000 1101
  519.           (logior 12 5 7)    ; 12  =  0000  0000 0000  0000 0000  0000 1100
  520.                              ;  5  =  0000  0000 0000  0000 0000  0000 0101
  521.                              ;  7  =  0000  0000 0000  0000 0000  0000 0111
  522.                => 15         ; 15  =  0000  0000 0000  0000 0000  0000 1111
  523.  - Function: logxor &rest INTS-OR-MARKERS
  524.      This function returns the "exclusive or" of its arguments: the Nth
  525.      bit is set in the result if, and only if, the Nth bit is set in an
  526.      odd number of the arguments.  If there are no arguments, the
  527.      result is 0, which is an identity element for this operation.  If
  528.      `logxor' is passed just one argument, it returns that argument.
  529.           ;               28-bit binary values
  530.           
  531.           (logxor 12 5)      ; 12  =  0000  0000 0000  0000 0000  0000 1100
  532.                              ;  5  =  0000  0000 0000  0000 0000  0000 0101
  533.                => 9          ;  9  =  0000  0000 0000  0000 0000  0000 1001
  534.           (logxor 12 5 7)    ; 12  =  0000  0000 0000  0000 0000  0000 1100
  535.                              ;  5  =  0000  0000 0000  0000 0000  0000 0101
  536.                              ;  7  =  0000  0000 0000  0000 0000  0000 0111
  537.                => 14         ; 14  =  0000  0000 0000  0000 0000  0000 1110
  538.  - Function: lognot INTEGER
  539.      This function returns the logical complement of its argument: the
  540.      Nth bit is one in the result if, and only if, the Nth bit is zero
  541.      in INTEGER, and vice-versa.
  542.           (lognot 5)
  543.                => -6
  544.           ;;  5  =  0000  0000 0000  0000 0000  0000 0101
  545.           ;; becomes
  546.           ;; -6  =  1111  1111 1111  1111 1111  1111 1010
  547. File: elisp,  Node: Math Functions,  Next: Random Numbers,  Prev: Bitwise Operations,  Up: Numbers
  548. Standard Mathematical Functions
  549. ===============================
  550.    These mathematical functions are available if floating point is
  551. supported.  They allow integers as well as floating point numbers as
  552. arguments.
  553.  - Function: sin ARG
  554.  - Function: cos ARG
  555.  - Function: tan ARG
  556.      These are the ordinary trigonometric functions, with argument
  557.      measured in radians.
  558.  - Function: asin ARG
  559.      The value of `(asin ARG)' is a number between -pi/2 and pi/2
  560.      (inclusive) whose sine is ARG; if, however, ARG is out of range
  561.      (outside [-1, 1]), then the result is a NaN.
  562.  - Function: acos ARG
  563.      The value of `(acos ARG)' is a number between 0 and pi (inclusive)
  564.      whose cosine is ARG; if, however, ARG is out of range (outside
  565.      [-1, 1]), then the result is a NaN.
  566.  - Function: atan ARG
  567.      The value of `(atan ARG)' is a number between -pi/2 and pi/2
  568.      (exclusive) whose tangent is ARG.
  569.  - Function: exp ARG
  570.      This is the exponential function; it returns e to the power ARG.
  571.      e is a fundamental mathematical constant also called the base of
  572.      natural logarithms.
  573.  - Function: log ARG &optional BASE
  574.      This function returns the logarithm of ARG, with base BASE.  If
  575.      you don't specify BASE, the base E is used.  If ARG is negative,
  576.      the result is a NaN.
  577.  - Function: log10 ARG
  578.      This function returns the logarithm of ARG, with base 10.  If ARG
  579.      is negative, the result is a NaN.  `(log10 X)' == `(log X 10)', at
  580.      least approximately.
  581.  - Function: expt X Y
  582.      This function returns X raised to power Y.  If both arguments are
  583.      integers and Y is positive, the result is an integer; in this
  584.      case, it is truncated to fit the range of possible integer values.
  585.  - Function: sqrt ARG
  586.      This returns the square root of ARG.  If ARG is negative, the
  587.      value is a NaN.
  588. File: elisp,  Node: Random Numbers,  Prev: Math Functions,  Up: Numbers
  589. Random Numbers
  590. ==============
  591.    A deterministic computer program cannot generate true random numbers.
  592. For most purposes, "pseudo-random numbers" suffice.  A series of
  593. pseudo-random numbers is generated in a deterministic fashion.  The
  594. numbers are not truly random, but they have certain properties that
  595. mimic a random series.  For example, all possible values occur equally
  596. often in a pseudo-random series.
  597.    In Emacs, pseudo-random numbers are generated from a "seed" number.
  598. Starting from any given seed, the `random' function always generates
  599. the same sequence of numbers.  Emacs always starts with the same seed
  600. value, so the sequence of values of `random' is actually the same in
  601. each Emacs run!  For example, in one operating system, the first call
  602. to `(random)' after you start Emacs always returns -1457731, and the
  603. second one always returns -7692030.  This repeatability is helpful for
  604. debugging.
  605.    If you want truly unpredictable random numbers, execute `(random
  606. t)'.  This chooses a new seed based on the current time of day and on
  607. Emacs's process ID number.
  608.  - Function: random &optional LIMIT
  609.      This function returns a pseudo-random integer.  Repeated calls
  610.      return a series of pseudo-random integers.
  611.      If LIMIT is a positive integer, the value is chosen to be
  612.      nonnegative and less than LIMIT.
  613.      If LIMIT is `t', it means to choose a new seed based on the
  614.      current time of day and on Emacs's process ID number.
  615.      On some machines, any integer representable in Lisp may be the
  616.      result of `random'.  On other machines, the result can never be
  617.      larger than a certain maximum or less than a certain (negative)
  618.      minimum.
  619. File: elisp,  Node: Strings and Characters,  Next: Lists,  Prev: Numbers,  Up: Top
  620. Strings and Characters
  621. **********************
  622.    A string in Emacs Lisp is an array that contains an ordered sequence
  623. of characters.  Strings are used as names of symbols, buffers, and
  624. files, to send messages to users, to hold text being copied between
  625. buffers, and for many other purposes.  Because strings are so important,
  626. Emacs Lisp has many functions expressly for manipulating them.  Emacs
  627. Lisp programs use strings more often than individual characters.
  628.    *Note Strings of Events::, for special considerations for strings of
  629. keyboard character events.
  630. * Menu:
  631. * Basics: String Basics.      Basic properties of strings and characters.
  632. * Predicates for Strings::    Testing whether an object is a string or char.
  633. * Creating Strings::          Functions to allocate new strings.
  634. * Text Comparison::           Comparing characters or strings.
  635. * String Conversion::         Converting characters or strings and vice versa.
  636. * Formatting Strings::        `format': Emacs's analog of `printf'.
  637. * Character Case::            Case conversion functions.
  638. * Case Table::              Customizing case conversion.
  639. File: elisp,  Node: String Basics,  Next: Predicates for Strings,  Up: Strings and Characters
  640. String and Character Basics
  641. ===========================
  642.    Strings in Emacs Lisp are arrays that contain an ordered sequence of
  643. characters.  Characters are represented in Emacs Lisp as integers;
  644. whether an integer was intended as a character or not is determined only
  645. by how it is used.  Thus, strings really contain integers.
  646.    The length of a string (like any array) is fixed and independent of
  647. the string contents, and cannot be altered.  Strings in Lisp are *not*
  648. terminated by a distinguished character code.  (By contrast, strings in
  649. C are terminated by a character with ASCII code 0.) This means that any
  650. character, including the null character (ASCII code 0), is a valid
  651. element of a string.
  652.    Since strings are considered arrays, you can operate on them with the
  653. general array functions.  (*Note Sequences Arrays Vectors::.)  For
  654. example, you can access or change individual characters in a string
  655. using the functions `aref' and `aset' (*note Array Functions::.).
  656.    Each character in a string is stored in a single byte.  Therefore,
  657. numbers not in the range 0 to 255 are truncated when stored into a
  658. string.  This means that a string takes up much less memory than a
  659. vector of the same length.
  660.    Sometimes key sequences are represented as strings.  When a string is
  661. a key sequence, string elements in the range 128 to 255 represent meta
  662. characters (which are extremely large integers) rather than keyboard
  663. events in the range 128 to 255.
  664.    Strings cannot hold characters that have the hyper, super or alt
  665. modifiers; they can hold ASCII control characters, but no other control
  666. characters.  They do not distinguish case in ASCII control characters.
  667. *Note Character Type::, for more information about representation of
  668. meta and other modifiers for keyboard input characters.
  669.    Strings are useful for holding regular expressions.  You can also
  670. match regular expressions against strings (*note Regexp Search::.).  The
  671. functions `match-string' (*note Simple Match Data::.) and
  672. `replace-match' (*note Replacing Match::.) are useful for decomposing
  673. and modifying strings based on regular expression matching.
  674.    Like a buffer, a string can contain text properties for the
  675. characters in it, as well as the characters themselves.  *Note Text
  676. Properties::.  All the Lisp primitives that copy text from strings to
  677. buffers or other strings also copy the properties of the characters
  678. being copied.
  679.    *Note Text::, for information about functions that display strings or
  680. copy them into buffers.  *Note Character Type::, and *Note String
  681. Type::, for information about the syntax of characters and strings.
  682. File: elisp,  Node: Predicates for Strings,  Next: Creating Strings,  Prev: String Basics,  Up: Strings and Characters
  683. The Predicates for Strings
  684. ==========================
  685.    For more information about general sequence and array predicates,
  686. see *Note Sequences Arrays Vectors::, and *Note Arrays::.
  687.  - Function: stringp OBJECT
  688.      This function returns `t' if OBJECT is a string, `nil' otherwise.
  689.  - Function: char-or-string-p OBJECT
  690.      This function returns `t' if OBJECT is a string or a character
  691.      (i.e., an integer), `nil' otherwise.
  692. File: elisp,  Node: Creating Strings,  Next: Text Comparison,  Prev: Predicates for Strings,  Up: Strings and Characters
  693. Creating Strings
  694. ================
  695.    The following functions create strings, either from scratch, or by
  696. putting strings together, or by taking them apart.
  697.  - Function: make-string COUNT CHARACTER
  698.      This function returns a string made up of COUNT repetitions of
  699.      CHARACTER.  If COUNT is negative, an error is signaled.
  700.           (make-string 5 ?x)
  701.                => "xxxxx"
  702.           (make-string 0 ?x)
  703.                => ""
  704.      Other functions to compare with this one include `char-to-string'
  705.      (*note String Conversion::.), `make-vector' (*note Vectors::.), and
  706.      `make-list' (*note Building Lists::.).
  707.  - Function: substring STRING START &optional END
  708.      This function returns a new string which consists of those
  709.      characters from STRING in the range from (and including) the
  710.      character at the index START up to (but excluding) the character
  711.      at the index END.  The first character is at index zero.
  712.           (substring "abcdefg" 0 3)
  713.                => "abc"
  714.      Here the index for `a' is 0, the index for `b' is 1, and the index
  715.      for `c' is 2.  Thus, three letters, `abc', are copied from the
  716.      string `"abcdefg"'.  The index 3 marks the character position up
  717.      to which the substring is copied.  The character whose index is 3
  718.      is actually the fourth character in the string.
  719.      A negative number counts from the end of the string, so that -1
  720.      signifies the index of the last character of the string.  For
  721.      example:
  722.           (substring "abcdefg" -3 -1)
  723.                => "ef"
  724.      In this example, the index for `e' is -3, the index for `f' is -2,
  725.      and the index for `g' is -1.  Therefore, `e' and `f' are included,
  726.      and `g' is excluded.
  727.      When `nil' is used as an index, it stands for the length of the
  728.      string.  Thus,
  729.           (substring "abcdefg" -3 nil)
  730.                => "efg"
  731.      Omitting the argument END is equivalent to specifying `nil'.  It
  732.      follows that `(substring STRING 0)' returns a copy of all of
  733.      STRING.
  734.           (substring "abcdefg" 0)
  735.                => "abcdefg"
  736.      But we recommend `copy-sequence' for this purpose (*note Sequence
  737.      Functions::.).
  738.      If the characters copied from STRING have text properties, the
  739.      properties are copied into the new string also.  *Note Text
  740.      Properties::.
  741.      A `wrong-type-argument' error is signaled if either START or END
  742.      is not an integer or `nil'.  An `args-out-of-range' error is
  743.      signaled if START indicates a character following END, or if
  744.      either integer is out of range for STRING.
  745.      Contrast this function with `buffer-substring' (*note Buffer
  746.      Contents::.), which returns a string containing a portion of the
  747.      text in the current buffer.  The beginning of a string is at index
  748.      0, but the beginning of a buffer is at index 1.
  749.  - Function: concat &rest SEQUENCES
  750.      This function returns a new string consisting of the characters in
  751.      the arguments passed to it (along with their text properties, if
  752.      any).  The arguments may be strings, lists of numbers, or vectors
  753.      of numbers; they are not themselves changed.  If `concat' receives
  754.      no arguments, it returns an empty string.
  755.           (concat "abc" "-def")
  756.                => "abc-def"
  757.           (concat "abc" (list 120 (+ 256 121)) [122])
  758.                => "abcxyz"
  759.           ;; `nil' is an empty sequence.
  760.           (concat "abc" nil "-def")
  761.                => "abc-def"
  762.           (concat "The " "quick brown " "fox.")
  763.                => "The quick brown fox."
  764.           (concat)
  765.                => ""
  766.      The second example above shows how characters stored in strings are
  767.      taken modulo 256.  In other words, each character in the string is
  768.      stored in one byte.
  769.      The `concat' function always constructs a new string that is not
  770.      `eq' to any existing string.
  771.      When an argument is an integer (not a sequence of integers), it is
  772.      converted to a string of digits making up the decimal printed
  773.      representation of the integer.  *Don't use this feature; we plan
  774.      to eliminate it.  If you already use this feature, change your
  775.      programs now!*  The proper way to convert an integer to a decimal
  776.      number in this way is with `format' (*note Formatting Strings::.)
  777.      or `number-to-string' (*note String Conversion::.).
  778.           (concat 137)
  779.                => "137"
  780.           (concat 54 321)
  781.                => "54321"
  782.      For information about other concatenation functions, see the
  783.      description of `mapconcat' in *Note Mapping Functions::, `vconcat'
  784.      in *Note Vectors::, and `append' in *Note Building Lists::.
  785. File: elisp,  Node: Text Comparison,  Next: String Conversion,  Prev: Creating Strings,  Up: Strings and Characters
  786. Comparison of Characters and Strings
  787. ====================================
  788.  - Function: char-equal CHARACTER1 CHARACTER2
  789.      This function returns `t' if the arguments represent the same
  790.      character, `nil' otherwise.  This function ignores differences in
  791.      case if `case-fold-search' is non-`nil'.
  792.           (char-equal ?x ?x)
  793.                => t
  794.           (char-to-string (+ 256 ?x))
  795.                => "x"
  796.           (char-equal ?x  (+ 256 ?x))
  797.                => t
  798.  - Function: string= STRING1 STRING2
  799.      This function returns `t' if the characters of the two strings
  800.      match exactly; case is significant.
  801.           (string= "abc" "abc")
  802.                => t
  803.           (string= "abc" "ABC")
  804.                => nil
  805.           (string= "ab" "ABC")
  806.                => nil
  807.      The function `string=' ignores the text properties of the two
  808.      strings.  To compare strings in a way that compares their text
  809.      properties also, use `equal' (*note Equality Predicates::.).
  810.  - Function: string-equal STRING1 STRING2
  811.      `string-equal' is another name for `string='.
  812.  - Function: string< STRING1 STRING2
  813.      This function compares two strings a character at a time.  First it
  814.      scans both the strings at once to find the first pair of
  815.      corresponding characters that do not match.  If the lesser
  816.      character of those two is the character from STRING1, then STRING1
  817.      is less, and this function returns `t'.  If the lesser character
  818.      is the one from STRING2, then STRING1 is greater, and this
  819.      function returns `nil'.  If the two strings match entirely, the
  820.      value is `nil'.
  821.      Pairs of characters are compared by their ASCII codes.  Keep in
  822.      mind that lower case letters have higher numeric values in the
  823.      ASCII character set than their upper case counterparts; numbers and
  824.      many punctuation characters have a lower numeric value than upper
  825.      case letters.
  826.           (string< "abc" "abd")
  827.                => t
  828.           (string< "abd" "abc")
  829.                => nil
  830.           (string< "123" "abc")
  831.                => t
  832.      When the strings have different lengths, and they match up to the
  833.      length of STRING1, then the result is `t'.  If they match up to
  834.      the length of STRING2, the result is `nil'.  A string of no
  835.      characters is less than any other string.
  836.           (string< "" "abc")
  837.                => t
  838.           (string< "ab" "abc")
  839.                => t
  840.           (string< "abc" "")
  841.                => nil
  842.           (string< "abc" "ab")
  843.                => nil
  844.           (string< "" "")
  845.                => nil
  846.  - Function: string-lessp STRING1 STRING2
  847.      `string-lessp' is another name for `string<'.
  848.    See also `compare-buffer-substrings' in *Note Comparing Text::, for
  849. a way to compare text in buffers.  The function `string-match', which
  850. matches a regular expression against a string, can be used for a kind
  851. of string comparison; see *Note Regexp Search::.
  852. File: elisp,  Node: String Conversion,  Next: Formatting Strings,  Prev: Text Comparison,  Up: Strings and Characters
  853. Conversion of Characters and Strings
  854. ====================================
  855.    This section describes functions for conversions between characters,
  856. strings and integers.  `format' and `prin1-to-string' (*note Output
  857. Functions::.) can also convert Lisp objects into strings.
  858. `read-from-string' (*note Input Functions::.) can "convert" a string
  859. representation of a Lisp object into an object.
  860.    *Note Documentation::, for functions that produce textual
  861. descriptions of text characters and general input events
  862. (`single-key-description' and `text-char-description').  These
  863. functions are used primarily for making help messages.
  864.  - Function: char-to-string CHARACTER
  865.      This function returns a new string with a length of one character.
  866.      The value of CHARACTER, modulo 256, is used to initialize the
  867.      element of the string.
  868.      This function is similar to `make-string' with an integer argument
  869.      of 1.  (*Note Creating Strings::.)  This conversion can also be
  870.      done with `format' using the `%c' format specification.  (*Note
  871.      Formatting Strings::.)
  872.           (char-to-string ?x)
  873.                => "x"
  874.           (char-to-string (+ 256 ?x))
  875.                => "x"
  876.           (make-string 1 ?x)
  877.                => "x"
  878.  - Function: string-to-char STRING
  879.      This function returns the first character in STRING.  If the
  880.      string is empty, the function returns 0.  The value is also 0 when
  881.      the first character of STRING is the null character, ASCII code 0.
  882.           (string-to-char "ABC")
  883.                => 65
  884.           (string-to-char "xyz")
  885.                => 120
  886.           (string-to-char "")
  887.                => 0
  888.           (string-to-char "\000")
  889.                => 0
  890.      This function may be eliminated in the future if it does not seem
  891.      useful enough to retain.
  892.  - Function: number-to-string NUMBER
  893.      This function returns a string consisting of the printed
  894.      representation of NUMBER, which may be an integer or a floating
  895.      point number.  The value starts with a sign if the argument is
  896.      negative.
  897.           (number-to-string 256)
  898.                => "256"
  899.           (number-to-string -23)
  900.                => "-23"
  901.           (number-to-string -23.5)
  902.                => "-23.5"
  903.      `int-to-string' is a semi-obsolete alias for this function.
  904.      See also the function `format' in *Note Formatting Strings::.
  905.  - Function: string-to-number STRING
  906.      This function returns the numeric value of the characters in
  907.      STRING, read in base ten.  It skips spaces and tabs at the
  908.      beginning of STRING, then reads as much of STRING as it can
  909.      interpret as a number.  (On some systems it ignores other
  910.      whitespace at the beginning, not just spaces and tabs.)  If the
  911.      first character after the ignored whitespace is not a digit or a
  912.      minus sign, this function returns 0.
  913.           (string-to-number "256")
  914.                => 256
  915.           (string-to-number "25 is a perfect square.")
  916.                => 25
  917.           (string-to-number "X256")
  918.                => 0
  919.           (string-to-number "-4.5")
  920.                => -4.5
  921.      `string-to-int' is an obsolete alias for this function.
  922.